package com.hyz.service.assetManager.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hyz.mapper.assetImport.AssetImportMapper;
import com.hyz.mapper.assetLog.AssetLogMapper;
import com.hyz.pojo.assetImport.AssetImport;
import com.hyz.pojo.assetImport.AssetImportEnum;
import com.hyz.pojo.assetLog.AssetLog;
import com.hyz.service.assetManager.AssetManagerService;
import com.hyz.util.GetValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pojo.permission.assetManager.AssetImportRequest;

import java.util.ArrayList;
import java.util.List;

/**
 * @author HGD
 * @date 2022/10/2 20:28
 */
@Service
public class AssetManagerServiceImpl implements AssetManagerService {
    @Autowired
    private GetValue getValue;
    @Autowired
    private AssetImportMapper assetImportMapper;
    @Autowired
    private AssetLogMapper assetLogMapper;

    @Override
    public void createAssetImport(AssetImportRequest request) {
        Long assetManagerId = request.getAssetManagerId();
        List<Long> assetIds = request.getAssetIds();
        Long repositoryId = request.getRepositoryId();

        List<AssetImport> assetImports = new ArrayList<>();
        List<AssetLog> assetLogs = new ArrayList<>();
        for (Long assetId : assetIds) {
            AssetImport assetImport = AssetImport.builder()
                    .id(getValue.getUuid())
                    .assetId(assetId)
                    .repositoryId(repositoryId)
                    .assetManagerId(assetManagerId)
                    .createTime(getValue.getCurrentTime())
                    .result(AssetImportEnum.create.getKey().longValue())
                    .build();
            assetImports.add(assetImport);
            AssetLog assetLog = AssetLog.builder()
                    .id(getValue.getUuid())
                    .assetId(assetImport.getAssetId())
                    .createTime(assetImport.getCreateTime())
                    .result(AssetImportEnum.create.getValue()).build();
            assetLogs.add(assetLog);
        }
        assetLogMapper.batchInsertAssetLog(assetLogs);
        assetImportMapper.batchCreateAssetImport(assetImports);
    }

    @Override
    public List<AssetImport> getAssetImportByUserId(Long assetManagerId) {
        QueryWrapper<AssetImport> wrapper = new QueryWrapper<>();
        wrapper.eq("asset_manager_id", assetManagerId)
                .eq("result", 0L);
        return assetImportMapper.selectList(wrapper);
    }
}
